#include "CSceneNodeAnimatorFlyCircle.h"
#include "CSceneManager.h"

ldx::scene::CSceneNodeAnimatorFlyCircle::CSceneNodeAnimatorFlyCircle(float time, const mt::vec3f & center, float radius, float speed, const mt::vec3f & direction, float radiusEllipsoid)
	: Center(center), Direction(direction), Radius(radius),
	RadiusEllipsoid(radiusEllipsoid), Speed(speed), StartTime(time)
{
#ifdef _DEBUG
	setDebugName("CSceneNodeAnimatorFlyCircle");
#endif
	init();
}

//! animates a scene node
void ldx::scene::CSceneNodeAnimatorFlyCircle::animateNode(ISceneNode* node, float timeMs)
{
	if (0 == node)
		return;

	float time;

	// Check for the condition where the StartTime is in the future.
	StartTime += timeMs;
	if (timeMs>0.f)
		time = StartTime * Speed;
	else
		time = -StartTime* Speed;

	//	node->setPosition(Center + Radius * ((VecU*cosf(time)) + (VecV*sinf(time))));
	float r2 = RadiusEllipsoid == 0.f ? Radius : RadiusEllipsoid;
	node->setPosition(Center + (Radius*cosf(time)*VecU) + (r2*sinf(time)*VecV));
}

ldx::scene::ISceneNodeAnimator * ldx::scene::CSceneNodeAnimatorFlyCircle::createClone(ISceneNode * node, CSceneManager * newManager)
{
	CSceneNodeAnimatorFlyCircle * newAnimator =
		new CSceneNodeAnimatorFlyCircle(StartTime, Center, Radius, Speed, Direction, RadiusEllipsoid);

	return newAnimator;
}

void ldx::scene::CSceneNodeAnimatorFlyCircle::init()
{
	Direction.normalize();

	if (Direction.Y != 0)
		VecV = mt::vec3f(50, 0, 0).crossProduct(Direction);
	else
		VecV = mt::vec3f(0, 50, 0).crossProduct(Direction); //mt::vec3(0, 50, 0).crossProduct(Direction).normalize();
	VecU = VecV.crossProduct(Direction); //VecV.crossProduct(Direction).normalize();
}
